package com.synergy.stockflow.warehouse.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.synergy.stockflow.warehouse.entity.InventoryCountDetail;
import com.synergy.stockflow.warehouse.entity.InventoryTransaction;
import com.synergy.stockflow.warehouse.service.InventoryTransactionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/inventory_sss")
public class InventoryTransactionController {
    @Autowired
    private InventoryTransactionService inventoryTransactionService;

    //查询数据库
    @GetMapping("/trees")
    private Map<String, Object> TreesAll() {
        Map<String, Object> result = new HashMap<>();
        try {
            List<InventoryTransaction> inventoryTransactions = inventoryTransactionService.list();
            result.put("success", true);
            result.put("data", inventoryTransactions);
            result.put("message", "查询成功");
        } catch (Exception a) {
            result.put("success", false);
            result.put("massage", "查询失败:" + a.getMessage());
        }
        return result;
    }

    @GetMapping
    public Map<String, Object> sag(
            @RequestParam(name = "current", defaultValue = "1") int current,
            @RequestParam(name = "size", defaultValue = "10") int size,
            @RequestParam(name = "transactionId", required = false) String transactionId,
            @RequestParam(name = "transactionType", required = false) String transactionType
    ) {
        Map<String, Object> result = new HashMap<>();
        try {
            QueryWrapper<InventoryTransaction> queryWrapper = new QueryWrapper<>();
            if (transactionId != null && transactionId.isEmpty()) {
                queryWrapper.lambda().like(InventoryTransaction::getTransactionId, transactionId);
            }
            if (transactionType != null && transactionType.isEmpty()) {
                queryWrapper.lambda().like(InventoryTransaction::getTransactionType, transactionType);
            }
            Page<InventoryTransaction> page = new Page<>(current, size);
            result.put("success", true);
            result.put("total", page.getPages());
            result.put("page", page.getPages());
            result.put("date", page.getRecords());
        } catch (Exception a) {
            result.put("success", false);
            result.put("massage", "分页查询失败:" + a.getMessage());
        }
        return result;
    }

    @PostMapping("/add")
    public Map<String, Object> addTransanction(@RequestBody List<InventoryTransaction> transactionList) {
        Map<String, Object> result = new HashMap<>();
        try {
            if (transactionList == null || transactionList.isEmpty()){
                result.put("success",false);
                result.put("message","流水数据不能为空");
                return result;
            }
            boolean saveSuccess = inventoryTransactionService.saveBatch(transactionList);
            result.put("success",saveSuccess);
            result.put("message",saveSuccess ? "流水保存成功":"流水保存失败");
        }catch (Exception e){
            result.put("success",false);
            result.put("message","流水表保存异常:"+e.getMessage());
        }
        return result;
    }
}
